// 有一个用例没通过
#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define DEBUG_
#ifdef DEBUG_
#define PF(...) printf(__VA_ARGS__)
#define FRE(x)                    \
  freopen("d:/oj/" #x ".in", "r", \
          stdin)  //,freopen("d:/oj/"#x".out","w",stdout)
#define FREC fclose(stdin), fclose(stdout);
#else
#define PF(...)
#define FRE(x)
#define FREC
#endif

class Solution {
 public:
  /**
   * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
   *
   *
   * @param s string字符串
   * @param k int整型
   * @return int整型
   */

  int longestSubstring(string s, int k) {
    // write code here
    unordered_map<char, int> map;
    queue<string> que;
    que.push(s);
    int cur = s.size();
    int maxNm = 0;
    while (cur > 0) {
      string s1 = que.front();
      que.pop();
      for (int i = 0; i < s1.size(); i++) {
        map[s1[i]]++;
      }
      string tmp;
      for (int i = 0; i < s1.size(); i++) {
        if (map[s1[i]] >= k) {
          tmp += s1[i];
          if (i == s1.size() - 1)
            if (tmp.size() >= k) que.push(tmp);
        } else {
          if (tmp.size() >= k) que.push(tmp);
          tmp.clear();
        }
      }
      map.clear();
      cur--;
    }

    while (que.size() > 0) {
      maxNm = que.front().size() > maxNm ? que.front().size() : maxNm;
      que.pop();
    }
    return maxNm;
  }
};
